home *** CD-ROM | disk | FTP | other *** search
- #ifndef RANDOM_H
- #define RANDOM_H 1
- //
- // Two-Stage Cryptographic Random Number Generator
- //
- // Copyright © 1996, Jon Callas, Eldacur Technologies. All Rights Reserved.
- //
- // You have permission to use this code in any commercial or non-commercial programs provided
- // you do the following:
- //
- // (1) Somewhere in your documentation, about box, etc. you state that you use Jon Callas's
- // Two-Stage Random Number Generator and that it is copyright by him.
- // (2) If you use it in a commercial product, you send me email at jon@worldbenders.com telling me
- // that you're using it. I don't have any nefarious purposes, I'm not going to sell your
- // name to some mailing list, I just want to know who is using it, so I can send updates
- // to you and be able to brag about the bizillion people that you use this.
- // (3) If you find bugs, make substantive improvements, etc. that you make a best-faith effort
- // to send them to me so I can incorporate it into the code.
- //
- // That's it. That's all you have to do. It's not much to ask. It's not as odious as a copyleft
- // so why not do it?
- //
-
- #include "shs.h"
-
- const int HASHLEN = 20;
- const int TOTALENTROPY = (HASHLEN * 8 * 10);
-
- typedef struct
- {
- long distillerEntropy;
- long numberPlace;
- long newPlace;
- long beenFullySeeded;
- SHS_CTX still;
- unsigned char numbers[256];
-
- } RandomState;
-
- class Distiller
- {
- protected:
- SHS_CTX shs;
- long totalEntropy;
-
- public:
- Distiller();
-
- int AddData(void *start, long length, int decibits);
- void GetHash(unsigned char hash[HASHLEN]);
- void Init();
- void SetEntropy(long decibits) { totalEntropy = decibits; };
- int GetEntropy(void) { return totalEntropy; };
- };
-
- class Grinder
- {
- protected:
- unsigned char numbers[256];
- long numberPlace;
- long newPlace;
-
- void InitNums();
-
- public:
- Grinder();
- Grinder(void *theStuff, long stuffLen);
-
- unsigned char Turn();
- void AddStuff(void *theStuff, long stuffLen);
-
- void GetRandom(void *thePlace, long length);
- };
-
-
- class Randomizer : public Distiller, public Grinder
- {
- protected:
- long busy;
- void Busy() { busy = 1; };
- void NotBusy() { busy = 0; };
-
- public:
- Randomizer();
- Randomizer(void *seed, int seedLen);
-
- void AddSeed(void *seed, int seedLen, int entropyGuessInDecibits);
- unsigned char Byte();
- unsigned short Word();
- unsigned long Long();
- void String(void *start, long byteLen) { GetRandom(start, byteLen); };
- long isBusy() { return busy; };
- void GetState(RandomState *state);
- void SetState(RandomState *state);
- };
-
- #endif